#include "stdafx.h"
#include "LitEditorSettingsMap.h"

namespace Lit
{
	namespace Editor
	{
		LIT_API std::ostream& operator << ( std::ostream& stream, const SettingsValue& val )
		{
			stream << "[" << val.first << ":" << val.second << "]";
			return stream;
		}

		SettingsMap::SettingsMap( const std::string& name_value_separator, const std::string& property_separator )
			: name_val_sep(name_value_separator), property_sep( property_separator ) {}
		SettingsMap::SettingsMap( const SettingsMap& lst ):map(lst.map),name_val_sep(lst.name_val_sep), property_sep( lst.property_sep ){}
		SettingsMap::~SettingsMap(){}
		
		bool SettingsMap::operator == ( const SettingsMap& rk ) const{
			if( rk.map.size() != map.size() ) return true;
			for( auto i=map.begin(); i!=map.end(); i++ )
			{
				if( !rk.hasValue( i->first ) ) return false;
				if( rk.getValue( i->first )->second != i->second.second ) return false;
			}
			return true;
		}
		bool SettingsMap::operator != ( const SettingsMap& rk ) const{
			return !(*this == rk);
		}
		SettingsMap& SettingsMap::updateValue( const std::string &name, const std::string& val )
		{
			if( !hasValue(name) )
				return *this;
			map[name].second = val;
			return *this;
		}
		SettingsMap& SettingsMap::setValue( const SettingsValue& val )
		{
			map[val.first] = val;
			return *this;
		}
		SettingsValue* SettingsMap::getValue( const std::string &name )
		{
			if( map.find(name) != map.end() ) 
				return &map[name];
			throw Lit::ItemMissing( name, "SettingsMap", EXC_LOC );
			return NULL;
		}
		const SettingsValue* SettingsMap::getValue( const std::string &name ) const
		{
			if( map.find(name) != map.end() ) 
				return &map.at(name);
			throw Lit::ItemMissing( name, "SettingsMap", EXC_LOC );
			return NULL;
		}
		bool SettingsMap::hasValue( const std::string &name ) const
		{
			return (map.find(name)!=map.end());
		}
		size_t SettingsMap::size() const{ return map.size(); }
		void SettingsMap::clear(){ map.clear(); }

		SettingsMap::itor SettingsMap::begin(){ return map.begin(); }
		SettingsMap::itor SettingsMap::end(){ return map.end(); }
		SettingsMap::const_itor SettingsMap::begin() const{ return map.begin(); }
		SettingsMap::const_itor SettingsMap::end() const{ return map.end(); }

		std::string SettingsMap::toString() const{
			std::string ret;
			for( auto i=map.begin(); i!=map.end(); i++ )
				ret += i->first + name_val_sep + i->second + property_sep;
			return ret;
		}
		
	};
};